package com.azhon.appupdate.manager

import android.content.Context
import android.os.Build
import android.provider.Settings
import android.text.TextUtils
import java.lang.reflect.Method


/**
 * @projectName 判断华为手机是否开启纯净模式
 * @author
 * @description:
 * @date : 2024/5/20 17:02
 * @version 1.0.0
 */
object HuahweiIInstallPure {

    val PURE_MODE_STATE = "purity_mode"
    private const val HARMONY_OS = "harmony"

    /**
     * 判断是否开启纯净模式
     *  0：开启，1：关闭
     */
    fun getVersionPure(context: Context): Int {
        var result = 1
        if (!isHarmonyOS()) {
            return result
        }
        try {
            result = Settings.Global.getInt(context.contentResolver, "purity_mode");
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return result
    }

    /**
    判断是否为鸿蒙系统
     */
    fun isHarmonyOS(): Boolean {
        try {
            val clz = Class.forName("com.huawei.system.BuildEx")
            val method = clz.getMethod("getOsBrand")
            return HARMONY_OS == method.invoke(clz)
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        return false
    }


    /**
     * 获取华为系统版本名称
     * @return String  返回值：harmony
     */
    fun osBandName(): String? {
        try {
            val clz = Class.forName("com.huawei.system.BuildEx")
            return clz.getMethod("getOsBrand").invoke(clz) as String
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return ""
    }

    /**
     * 获取华为系统版本号
     * @return 版本号
     */
    fun harmonyOsv(): String? {
        return getProp("hw_sc.build.platform.version", "")
    }

    private fun getProp(property: String, defaultValue: String): String? {
        try {
            val spClz = Class.forName("android.os.SystemProperties")
            val method: Method = spClz.getDeclaredMethod("get", String::class.java)
            val value = method.invoke(spClz, property) as String
            return if (TextUtils.isEmpty(value)) {
                defaultValue
            } else value
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        return defaultValue
    }


}